package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

import cn.edu.neusoft.wuguangsheng.market.R;

public class  DituActivity extends AppCompatActivity {
    private Context context;
    private BaiduMap mBaiduMap;
    private MapView mMapView;
    // 自定义定位图标
    private BitmapDescriptor mIconLocation;
    private MyLocationConfiguration.LocationMode mLocationMode;

    //定位相关
    private LocationClient mLocationClient;
    private MyLocationListener mLocationListener;
    private boolean isFirstIn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_ditu);
        this.context = this;
        initView();

        initLocation();
    }
    //初始化定位
    private void initLocation() {

        //初始化定位显示的图标
        mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_openmap_mark);
        mLocationMode = MyLocationConfiguration.LocationMode.NORMAL;
        mLocationClient = new LocationClient(this);
        mLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mLocationListener);

        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");//设置返回值的坐标类型
        option.setIsNeedAddress(true);//是否获取具体位置
        option.setOpenGps(true);//是否打开GPS
        option.setScanSpan(1000);//设置多久更新一次位置

        mLocationClient.setLocOption(option);
    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.id_bmapView);
        mBaiduMap = mMapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
        mBaiduMap.setMapStatus(msu);
        //设置地图类型为卫星地图(不设置的话默认显示普通地图)
        //mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //开始定位
        mBaiduMap.setMyLocationEnabled(true);
        if(!mLocationClient.isStarted())
            mLocationClient.start();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //将MapView的生命周期与Activity的生命周期绑定 Activity存在MapView就存在
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //停止定位
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
/*
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main,menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {

       switch (item.getItemId()){
           case R.id.id_map_common:
               mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
               break;
           case R.id.id_map_site:
               mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
               break;
           default:
               break;
       }

       return super.onOptionsItemSelected(item);
   }*/

    private class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            MyLocationData data = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude())
                    .build();

            mBaiduMap.setMyLocationData(data);

            //设置自定义图标
            MyLocationConfiguration configuration = new MyLocationConfiguration(mLocationMode,true,mIconLocation);
            mBaiduMap.setMyLocationConfigeration(configuration);

            if(isFirstIn){
                LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
                MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
                mBaiduMap.animateMapStatus(msu);
                isFirstIn = false;
                //Toast.makeText(context,location.getAddrStr(),Toast.LENGTH_SHORT).show();
            }
        }
    }


}
